import requests
from bs4 import BeautifulSoup
import pymysql

conn = pymysql.connect(host="localhost", port=3306, user="root", passwd="abc123", db="db_word")

cursor = conn.cursor()

sql = """
    SELECT id,body FROM article
"""
cursor.execute(sql)
results = cursor.fetchall()

for id_,body in [(_[0],_[1]) for _ in results]:
    soup = BeautifulSoup(body,'lxml')
    abstract = ''.join([_.prettify() for _ in soup.find_all('p',limit=1)])
    sql_ = """
        UPDATE article SET abstract = %s WHERE id = %s
    """
    values = (abstract,id_)
    cursor.execute(sql_,values)
    conn.commit()
# for url,id_ in [(_[1],_[0]) for _ in results]:
#     res = requests.get(url)
#     soup = BeautifulSoup(res.text,'lxml')
#     print(res.text)
#     if res.text:
#         try:
#             headpic = soup.find('figure',attrs={'class':'wp-caption'}).find('img').get('src')
#             body = soup.find('div',attrs={'class':'dable-content-wrapper'})
#             if body:
#                 scripts = body.find_all('script')
#                 for script in scripts:
#                     script.extract()
#
#             sql_ = """
#                 UPDATE article SET body = %s,headpic = %s WHERE id = %s
#             """
#             values = (body.prettify() if body else None,headpic,id_)
#             cursor.execute(sql_,values)
#             conn.commit()
#         except:
#             ...

# print(title)

# <div class="elementor-widget-container">

# url = 'https://www.utusan.com.my/'
# res = requests.get(url)
# soup = BeautifulSoup(res.text, 'lxml')
# sources = soup.find_all('h3', attrs={'class': 'jeg_post_title'})
# rootsources = [_.find('a') for _ in sources]

# for text,href in zip([_.text for _ in rootsources],[_.get('href') for _ in rootsources]):
#     sql = """
#         INSERT INTO article (title, source, source_href) VALUES (%s,%s,%s)
#     """
#     values = (text,'https://www.utusan.com.my/',href)
#     cursor.execute(sql,values)
#     conn.commit()
